<!-- 储存工商信息相关控件 -->
<template>
  <!-- 法定代表人 -->
  <LegalRepresentative
    v-if="code === 'LEGAL_REPRESENTATIVE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </LegalRepresentative>

  <!-- 法定代表人 -->
  <CreditCode
    v-if="code === 'CREDIT_CODE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </CreditCode>

  <!-- 经营状态 -->
  <BusinessStatus
    v-if="code === 'BUSINESS_STATUS'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </BusinessStatus>

  <!-- 注册资本 -->
  <RegisteredCapital
    v-if="code === 'REGISTERED_CAPITAL'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </RegisteredCapital>

  <!-- 注册资本 -->
  <PaidCapital
    v-if="code === 'PAID_CAPITAL'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PaidCapital>

  <!-- 注册工商号 -->
  <BusinessNumber
    v-if="code === 'BUSINESS_NUMBER'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </BusinessNumber>

  <!-- 企业类型 -->
  <EnterpriseType
    v-if="code === 'ENTERPRISE_TYPE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </EnterpriseType>

  <!-- 成立时间 -->
  <EstablishedTime
    v-if="code === 'ESTABLISHED_TIME'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </EstablishedTime>

  <!-- 成立时间 -->
  <ApprovalDate
    v-if="code === 'APPROVAL_DATE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </ApprovalDate>

  <!-- 纳税人识别号 -->
  <TaxpayerNumber
    v-if="code === 'TAXPAYER_NUMBER'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </TaxpayerNumber>

  <!-- 纳税人资质 -->
  <TaxpayerQualification
    v-if="code === 'TAXPAYER_QUALIFICATION'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </TaxpayerQualification>

  <!-- 所属行业 -->
  <Industry
    v-if="code === 'INDUSTRY'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </Industry>

  <!-- 营业期限 -->
  <BusinessTerm
    v-if="code === 'BUSINESS_TERM'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </BusinessTerm>

  <!-- 所属地区 -->
  <Region
    v-if="code === 'REGION'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </Region>

  <!-- 登记机关 -->
  <Registrar
    v-if="code === 'REGISTRAR'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </Registrar>

  <!-- 人员规模 -->
  <PersonnelSize
    v-if="code === 'PERSONNEL_SIZE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </PersonnelSize>

  <!-- 参保人数 -->
  <InsuredNumber
    v-if="code === 'INSURED_NUMBER'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </InsuredNumber>

  <!-- 企业地址 -->
  <EnterpriseAddress
    v-if="code === 'ENTERPRISE_ADDRESS'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </EnterpriseAddress>

  <!-- 经营范围 -->
  <BusinessNature
    v-if="code === 'BUSINESS_NATURE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </BusinessNature>

  <!-- 营业执照 -->
  <BusinessLicense
    v-if="code === 'BUSINESS_LICENSE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </BusinessLicense>
</template>

<script setup lang="ts">
const props = withDefaults(
  //参数说明
  defineProps<{
    componentType: string;
    code: string;
    props: { [propName: string]: any };
    name?: string;
    required: boolean;
    disabled?: boolean;
    values: any;
    regex?: {
      [name: string]: {
        role: any;
        name: any;
      };
    };
  }>(),
  {}
);

// console.log(props);

const emit = defineEmits(['update:values']);
//复制一份props
const getProps = computed(() => {
  return props.props;
});

const validator = (value: any, callback: any) => {
  const { componentType, name, values, required, regex } = props;
  if (componentType === 'TEXT') {
    //单行输入框
    const { validate } = getProps.value;
    if (required && !values[0]) return callback('请输入' + name);
    if (!validate) return callback();

    if (regex) {
      const roleItem = regex[validate];
      const checkVal = roleItem.role.test(values[0]);
      if (!checkVal) {
        return callback(new Error('请输入正确的' + roleItem.name));
      }
    }
  } else if (componentType === 'TEXT_AREA') {
    //多行输入框
    if (required && !values[0]) return callback(`请输入${name}`);
  } else if (componentType === 'NUMBER') {
    if (required && !values[0]) return callback(`请输入${name}`);
  } else if (componentType === 'DATE' || componentType === 'DATE_RANGE') {
    if (required && !values[0]) return callback(`请选择日期`);
  }

  callback();
};
//当value只用到第一个绑定这个参数
const oneValue = computed({
  get() {
    return props.values?.length ? props.values[0] : '';
  },
  set(val) {
    emit('update:values', val);
  }
});

// 多选双向绑定
const checkGroupValue = computed({
  get() {
    return props.values;
  },
  set(val) {
    emit('update:values', val);
  }
});

// 储存数组对象的values
// const objArrValues = computed({
//   get() {
//     return props.values;
//   },
//   set(val) {
//     emit('update:values', val);
//   },
// });

// const getDepartmentList = () => {};
</script>

<style></style>
